#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int nums[4];
int ques[20][4];
int re[20][2];

void hhh()
{
	int i, j;
	int ques_num;
	scanf("%d", &ques_num);
	for (i=0;i<ques_num;i++)
	{
		int que;
		scanf("%d", &que);
		ques[i][0] = que / 1000;
		ques[i][1] = que / 100 % 10;
		ques[i][2] = que / 10 % 10;
		ques[i][3] = que % 10;
		for (j = 0; j < 4; j++)
		{
			if (ques[i][j] == nums[j])
			{
				re[i][0]++;
			}
			else
			{
				int p = 0;
				for (p = 0; p < 4; p++)
				{
					if (p != j && ques[i][j] == nums[p])
					{
						re[i][1]++;
						break;
					}
				}
			}

		}

	}

	printf("%dA%dB", re[0][0], re[0][1]);
	for (i = 1; i < ques_num; i++)
	{
		printf(" %dA%dB", re[i][0], re[i][1]);
	}
	printf("\n");
}

int main()
{
	int n;
	while (1)
	{
		scanf("%d", &n);
		if (n == 0)
			break;
		else
		{
			nums[0] = n / 1000;
			nums[1] = n / 100 % 10;
			nums[2] = n / 10 % 10;
			nums[3] = n % 10;
			memset(re, 0, sizeof(re));

			hhh();
		}
	}


	return 0;
}